package com.lib.core.utils;

import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Lifecycle;

/**
 * Created by youjun on 16/10/11.
 */

public class TabFragmentManager implements View.OnClickListener {

    private View[] tabViews;

    private FragmentManager fragmentManager;

    private Fragment currentFragment;

    private Fragment[] fragments;

    private int layoutID;

    private int selectPosition;

    private OnCanSelectTabListener onCanSelectTabListener;
    private OnSelectTabListener onSelectTabListener;

    public void setOnSelectTabListener(OnSelectTabListener onSelectTabListener) {
        this.onSelectTabListener = onSelectTabListener;
    }

    public void setOnCanSelectTabListener(OnCanSelectTabListener onCanSelectTabListener) {
        this.onCanSelectTabListener = onCanSelectTabListener;
    }

    public int getSelectPosition() {
        return selectPosition;
    }

    public TabFragmentManager(View[] tabViews, Fragment[] fragments, FragmentManager fragmentManager, int viewGropId) {
        this.tabViews = tabViews;
        this.fragments = fragments;
        this.fragmentManager = fragmentManager;
        this.layoutID = viewGropId;
        for (int i = 0; i < tabViews.length; i++) {
            tabViews[i].setTag(i);
            tabViews[i].setOnClickListener(this);
        }
    }

    private void showFragment(int tag) {
        Fragment fragment = fragmentManager.findFragmentByTag(tag + "");
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (fragment == null) {
            fragment = fragments[tag];
            transaction.add(layoutID, fragment, tag + "");
        }

        if (currentFragment != null) {
            transaction.hide(currentFragment);
//            currentFragment.setUserVisibleHint(false);
            transaction.setMaxLifecycle(currentFragment, Lifecycle.State.STARTED);
            transaction.show(fragment);
        }
        transaction.commit();
//        fragment.setUserVisibleHint(true);
        transaction.setMaxLifecycle(fragment, Lifecycle.State.RESUMED);
        currentFragment = fragment;
    }


    /**
     * 根据Tag显示页面
     */
    public void showSelect(int tag) {
        this.selectPosition = tag;
        boolean b = true;
        if (onCanSelectTabListener != null) {
             b = onCanSelectTabListener.onCanSelectTab(selectPosition);
        }
        if (b) {
            setMainBottomSelected();
            if (onSelectTabListener != null)
                onSelectTabListener.onSelectTab(selectPosition);
            tabViews[tag].setSelected(true);
            showFragment(tag);
        }
    }



    /**
     * 将所有的页面效果都清除
     */
    private void setMainBottomSelected() {
        for (View tabView : tabViews) {
            tabView.setSelected(false);
        }
    }

    @Override
    public void onClick(View v) {
        int tag = (int) v.getTag();
        showSelect(tag);
    }

    public interface OnCanSelectTabListener {
        boolean onCanSelectTab(int position);
    }

    public interface OnSelectTabListener {
        void onSelectTab(int position);
    }
}
